-- LocationAgent
-- Created by lijh 2016/11/22
-- 封装 Android 定位接口

module("LocationAgent", package.seeall);

local PURE_FILE = "LocationAgent";

-- Android 定位授权状态码
STATUS_NOT_DETERMINED = 0;            -- 用户尚未决定是否授权
STATUS_GRANTED = 1;                   -- 已获得授权
STATUS_DENIED = 2;                    -- 用户先前拒绝过授权
STATUS_DISABLED = 3;                  -- 用户先前拒绝过授权，并勾选了“不再提醒”

-- 定位超时时间
local locationTimeout = 20;

-- 内部函数声明
local onLocationPermissionGranted;
local onLocationPermissionDenied;
local onLocationPermissionDisabled;
local onLocationSuccess;
local onLocationFailed;

-- 初始化
function init()
     -- 注册获得定位授权的回调
    SdkMgr.registerJavaCallback("onLocationPermissionGranted", onLocationPermissionGranted);

     -- 注册用户拒绝定位授权的回调
    SdkMgr.registerJavaCallback("onLocationPermissionDenied", onLocationPermissionDenied);

     -- 注册用户拒绝定位授权的回调（之后不再弹出授权窗口）
    SdkMgr.registerJavaCallback("onLocationPermissionDisabled", onLocationPermissionDisabled);

    -- 注册定位成功的回调
    SdkMgr.registerJavaCallback("onLocationSuccess", onLocationSuccess);

    -- 注册定位失败的回调
    SdkMgr.registerJavaCallback("onLocationFailed", onLocationFailed);

    -- 初始化
    callJavaFuncWithClass("com.qcplay.location.LocationDelegateHelper", "locationInit", { locationTimeout }, "(I)V");
end

-- 判断定位服务是否开启
function isServiceEnabled()
    local ret = callJavaFuncWithClass("com.qcplay.location.LocationDelegateHelper", "isServiceEnabled", {}, "()Z");

    return ret;
end

-- 跳转到 GPS 设置页面
function enableService()
    callJavaFuncWithClass("com.qcplay.location.LocationDelegateHelper", "enableService", {}, "()V");
end

-- 打开权限设置窗口
function openPemissionSetting()
    callJavaFuncWithClass("com.qcplay.location.LocationDelegateHelper", "openPemissionSetting", {}, "()V");
end

-- 获取定位权限状态
function getPermissionStatus()
    local status = callJavaFuncWithClass("com.qcplay.location.LocationDelegateHelper", "getPermissionStatus", {}, "()I");

    return status;
end

-- 请求定位权限
function requestPermissions()
    callJavaFuncWithClass("com.qcplay.location.LocationDelegateHelper", "requestPermissions", {}, "()V");
end

-- 发起定位
function requestLocation()
    callJavaFuncWithClass("com.qcplay.location.LocationDelegateHelper", "requestLocation", {}, "()V");
end

-- 获得定位授权
function onLocationPermissionGranted()
    trace(PURE_FILE, "locatoin permission granted");
end

-- 定位授权被拒绝
function onLocationPermissionDenied()
    trace(PURE_FILE, "locatoin permission denied");
    EventMgr.fire(event.ANDROID_LOCATION_PERMISSION_DENIED);
end

-- 定位授权被拒绝（之后不再弹出授权窗口）
function onLocationPermissionDisabled()
    trace(PURE_FILE, "locatoin permission disabled");
    EventMgr.fire(event.ANDROID_LOCATION_PERMISSION_DISABLED);
end

-- 获取定位成功
function onLocationSuccess(param)
    trace(PURE_FILE, "onLocationSuccess:param = " .. param);

    local ret = string.parseJsonToTable(param);
    local longitude = ret["longitude"];
    local latitude  = ret["latitude"];
    local altitude  = ret["altitude"];

    trace(PURE_FILE, string.format("location success: longitude:%f, latitude:%f, altitude:%f", longitude, latitude, altitude));
    LocationM.onSuccess(latitude, longitude);
end

-- 获取定位失败
function onLocationFailed(param)
    local ret = string.parseJsonToTable(param);
    local errCode = ret["errCode"];

    trace(PURE_FILE, "location failed with errror code: %d", errCode);
    LocationM.onFail(tostring(errCode));
end